package com.karaoke.android.kege.databinding;

import android.databinding.BindingAdapter;
import android.databinding.adapters.ListenerUtil;
import android.databinding.adapters.ViewBindingAdapter;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.os.Environment;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.ListAdapter;
import android.widget.ListView;

import com.bumptech.glide.Glide;
import com.github.johnkil.print.PrintDrawable;
import com.gturedi.views.StatefulLayout;
import com.karaoke.android.data.domain.DataDictionary;
import com.karaoke.android.kege.R;
import com.karaoke.android.kege.widget.MenuItem;

import java.io.File;

import rx.Observable;
import rx.Scheduler;
import rx.functions.Func0;
import rx.schedulers.Schedulers;


/**
 * Created by allen on 2017/4/19.
 */

public class DataBindingAdapter
{
    @BindingAdapter("listAdapter")
    public static void setListAdapter(ListView listView, ListAdapter adapter)
    {
        listView.setAdapter(adapter);
    }

    @BindingAdapter("onItemSelectedListener")
    public static void onItemSelectedListener(ListView listView, AdapterView
            .OnItemSelectedListener listener)
    {
        listView.setOnItemSelectedListener(listener);
    }

    @BindingAdapter("onItemClickListener")
    public static void onItemClickListener(ListView listView, AdapterView.OnItemClickListener
            listener)
    {
        listView.setOnItemClickListener(listener);
    }

    @BindingAdapter("addItemDecoration")
    public static void addItemDecoration(RecyclerView recyclerView,
                                         RecyclerView.ItemDecoration decoration)
    {
        recyclerView.addItemDecoration(decoration);
    }




    @BindingAdapter("showLoading")
    public static void showLoading(StatefulLayout layout, boolean isShow)
    {
        if (isShow)
        { layout.showLoading("加载中..."); }
        else { layout.showContent(); }
    }

    @BindingAdapter(value = {"showEmpty", "emptyText"}, requireAll = false)
    public static void showEmpty(StatefulLayout layout, boolean isShow, String msg)
    {
        if (isShow)
        {
            if (msg != null)
            { layout.showEmpty(msg); }
            else { layout.showEmpty(); }
        }
    }

    @BindingAdapter(value = {"showError", "errorListener", "errorText"}, requireAll = false)
    public static void showError(StatefulLayout layout, boolean isShow, View.OnClickListener
            listener, String msg)
    {
        if (isShow)
        {
            if (msg != null)
            {
                layout.showError(msg, listener);
            }
            else { layout.showError(listener); }
        }
    }

    @BindingAdapter("icon")
    public static void setIcon(ImageView imageView, String code)
    {
        Drawable drawable = new PrintDrawable.Builder(imageView.getContext())
                .iconText(code).iconSizeDp(30).iconColorRes(R.color.md_indigo_100).build();
        imageView.setImageDrawable(drawable);
    }

    @BindingAdapter("menuItem")
    public static void menuItem(MenuItem menuItem, DataDictionary item)
    {
        menuItem.setItem(item);
    }

    @BindingAdapter("selectedPosition")
    public static void setSelectedPosition(ListView listView, Integer pos)
    {
        listView.requestFocus();
        listView.setItemChecked(pos, true);
    }


    @BindingAdapter("android:onViewAttachedToWindow")
    public static void setListener(View view, ViewBindingAdapter.OnViewAttachedToWindow attached)
    {
        setListener(view, null, attached);
    }

    @BindingAdapter("android:onViewDetachedFromWindow")
    public static void setListener(View view, ViewBindingAdapter.OnViewDetachedFromWindow detached)
    {
        setListener(view, detached, null);
    }

    @BindingAdapter({"android:onViewDetachedFromWindow", "android:onViewAttachedToWindow"})
    public static void setListener(View view, final ViewBindingAdapter.OnViewDetachedFromWindow
            detach,
                                   final ViewBindingAdapter.OnViewAttachedToWindow attach)
    {
        final View.OnAttachStateChangeListener newListener;
        if (detach == null && attach == null)
        {
            newListener = null;
        }
        else
        {
            newListener = new View.OnAttachStateChangeListener()
            {
                @Override
                public void onViewAttachedToWindow(View v)
                {
                    if (attach != null)
                    {
                        attach.onViewAttachedToWindow(v);
                    }
                }

                @Override
                public void onViewDetachedFromWindow(View v)
                {
                    if (detach != null)
                    {
                        detach.onViewDetachedFromWindow(v);
                    }
                }
            };
        }
        final View.OnAttachStateChangeListener oldListener = ListenerUtil.trackListener(view,
                newListener, R.id.onAttachStateChangeListener);
        if (oldListener != null)
        {
            view.removeOnAttachStateChangeListener(oldListener);
        }
        if (newListener != null)
        {
            view.addOnAttachStateChangeListener(newListener);
        }
    }

    @BindingAdapter("position")
    public static void setPosition(GridView gridView, int position)
    {
        gridView.smoothScrollToPosition(position);
    }
}

